package com.kin207.netty.listener.session;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.kin207.netty.listener.ListenerConfig;
import com.kin207.netty.listener.SessionListener;
import com.kin207.netty.session.Session;

/**
 * @author kin207
 *
 */
public class MaxSessionListener implements SessionListener {
	final static Logger logger = LoggerFactory.getLogger(MaxSessionListener.class);
	private int maxSession;
	
	public void initialize(ListenerConfig cfg) {
		if(cfg==null){
			return ;
		}
		String max = cfg.getParam("maxSession");
		if(max!=null && !"".equals(max.trim())){
			maxSession = Integer.parseInt(max);
			logger.info("max session {}", maxSession);
		}
	}

	public void created(Session session) {
		if(maxSession<1){
			return;
		}
		if(session.getSessionManager().sessions()>maxSession){
			session.getChannelHandlerContext().close();
			//session.getSessionManager().close(session.getChannelHandlerContext());
		}
		
	}

	public void destroyed(Session session) {
		
	}

	public int getMaxSession() {
		return maxSession;
	}

	public void setMaxSession(int maxSession) {
		this.maxSession = maxSession;
	}
	

}
